﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;

namespace BugReportingSystem
{
    public partial class Truongphong : Form
    {
        SqlConnection conn;
        SqlDataAdapter da, da1, da2, da3, da4,da5;
        SqlCommand cmd, cmd1, cmd2, cmd3, cmd4,cmd5,cmd6;
        SqlCommandBuilder cmb, cmb1;
        DataSet ds, ds1, ds2, ds3, ds4,ds5;
        DataTable dt, dt1, dt2, dt3, dt4,dt5;
        public Truongphong()
        {
            InitializeComponent();
        }

        private void Truongphong_Load(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConStr"]);
            cmd = new SqlCommand("SELECT * FROM Phongban", conn);
            conn.Open();
            da = new SqlDataAdapter(cmd);
            ds = new DataSet();
            da.Fill(ds, "LoaiSP");
            dt = ds.Tables["LoaiSP"];
            cbPhongban.DataSource = dt;
            cbPhongban.DisplayMember = "pbTenphongban";
            cbPhongban.ValueMember = "pbID";
            cbPhongban.Text = "(Chọn)";
            conn.Close();
            //danh sach don da phan viec
            cmd5 = new SqlCommand("select a.dID,a.kID, b.kTenkhachhang, a.sID, c.sTensanpham, a.dLydokhieunai, a.dNgaylap, d.pbTenphongban, a.dNgayhentra, e.nvTendaydu, a.dTrangthai from Donkhieunai a, Khachhang b, Sanpham c, Phongban d, Nhanvien e where a.kID= b.kID and a.sID=c.sID and a.pbID=d.pbID and a.nvID = e.nvID and a.pbID=3;", conn);
            conn.Open();
            da5 = new SqlDataAdapter(cmd5);
            ds5 = new DataSet();
            da5.Fill(ds5, "Da phan viec");
            dgvDaphanviec.DataSource = ds5.Tables["Da phan viec"];
            conn.Close();
            //danh sach don chua phan viec
            cmd1 = new SqlCommand("select a.dID,a.kID, b.kTenkhachhang, a.sID, c.sTensanpham, a.dLydokhieunai, a.dNgaylap, d.pbTenphongban, a.dNgayhentra, a.dTrangthai from Donkhieunai a, Khachhang b, Sanpham c, Phongban d where a.kID= b.kID and a.sID=c.sID and a.pbID=d.pbID and a.nvID is null and a.pbID=3;", conn);
            conn.Open();
            da1 = new SqlDataAdapter(cmd1);
            ds1= new DataSet();
            da1.Fill(ds1, "Khieu nai");
            dgvDonchuaphanviec.DataSource = ds1.Tables["Khieu nai"];
            conn.Close();

            cmd3 = new SqlCommand("Select * from Nhanvien",conn);
            conn.Open();
            da3 = new SqlDataAdapter(cmd3);
            ds3 = new DataSet();
            da3.Fill(ds3, "Nhan vien");
            dt3 = ds3.Tables["Nhan vien"];
            cbNhanvien.DataSource = dt3;
            cbNhanvien.DisplayMember = "nvTendaydu";
            cbNhanvien.ValueMember = "nvID";
            cbNhanvien.Text = "(Chọn)";
            conn.Close();

        }

        private void btOK_Click(object sender, EventArgs e)
        {
            string pbID = cbPhongban.SelectedValue.ToString();
            cmd2 = new SqlCommand("select a.dID, a.kID, b.kTenkhachhang, a.sID, c.sTensanpham, a.dLydokhieunai, a.dNgaylap, d.pbTenphongban, a.dNgayhentra, a.dTrangthai from Donkhieunai a, Khachhang b, Sanpham c, Phongban d where a.kID= b.kID and a.sID=c.sID and a.pbID=d.pbID and a.pbID="+ pbID +" and a.nvID is null ;", conn);
            conn.Open();
            da2 = new SqlDataAdapter(cmd2);
            ds2 = new DataSet();
            da2.Fill(ds2, "Khieunai");
            dgvDonchuaphanviec.DataSource = ds2.Tables["Khieunai"];
            conn.Close();
        }

        private void btCheck_Click(object sender, EventArgs e)
        {
            string nvID = cbNhanvien.SelectedValue.ToString();
           // MessageBox.Show("" + nvID);
            //cmd4 = new SqlCommand("select a.dID, b.kTenkhachhang, a.sID, c.sTensanpham, a.dLydokhieunai, a.dNgaylap, d.pbTenphongban, a.dNgayhentra, e.nvTendaydu, a.dTrangthai from Donkhieunai a, Khachhang b, Sanpham c, Phongban d, Nhanvien e where a.kID= b.kID and a.sID=c.sID and a.pbID=d.pbID and a.nvID = e.nvID and a.nvID=" + nvID + ";", conn);
            //conn.Open();
            cmd4 = new SqlCommand("select * from Donkhieunai where nvID='"+ nvID +"' and dTrangthai= 'false';",conn);
            conn.Open();
            da4 = new SqlDataAdapter(cmd4);
            ds4 = new DataSet();
            da1.Fill(ds4, "Du an thuc hien");
            dt4 = ds4.Tables["Du an thuc hien"];
            String count = ds4.Tables["Du an thuc hien"].DefaultView.Count.ToString();
            MessageBox.Show("Nhân viên này đang thực hiện " + count + " dự án.");
            conn.Close();
        }

        private void dgv1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int dong = e.RowIndex;
            txtMaKH.Text = dgvDonchuaphanviec.Rows[dong].Cells["MaKH"].Value.ToString();
            txtMaSP.Text = dgvDonchuaphanviec.Rows[dong].Cells["sID"].Value.ToString();
            txtLydo.Text = dgvDonchuaphanviec.Rows[dong].Cells["dLydokhieunai"].Value.ToString();
            txtNgaylap.Text = dgvDonchuaphanviec.Rows[dong].Cells["dNgaylap"].Value.ToString();
        }

        private void btDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btLuu_Click(object sender, EventArgs e)
        {
            conn.Open();
            int dong = dgvDonchuaphanviec.CurrentRow.Index;
            int dID =  int.Parse(dgvDonchuaphanviec.Rows[dong].Cells["dID"].Value.ToString());
            SqlCommand cmd6 = new SqlCommand("UPDATE Donkhieunai set nvID = '" + cbNhanvien.ValueMember + "',dNgayhentra='" + dateTimePicker1.Text + "' WHERE dID=" + dID + "", conn);
            cmd6.ExecuteNonQuery();
            MessageBox.Show("Lưu thông tin thành công!");
            conn.Close();
        }
    }
}
